home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / include / glibmm-2.4 / glibmm / main.h < prev    next >
Encoding:
C/C++ Source or Header  |  2006-04-20  |  25.7 KB  |  739 lines

  1. // -*- c++ -*-
  2. #ifndef _GLIBMM_MAIN_H
  3. #define _GLIBMM_MAIN_H
  4.  
  5. /* $Id: main.h,v 1.9 2005/03/14 15:22:13 murrayc Exp $ */
  6.  
  7. /* Copyright (C) 2002 The gtkmm Development Team
  8.  *
  9.  * This library is free software; you can redistribute it and/or
  10.  * modify it under the terms of the GNU Library General Public
  11.  * License as published by the Free Software Foundation; either
  12.  * version 2 of the License, or (at your option) any later version.
  13.  *
  14.  * This library is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17.  * Library General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU Library General Public
  20.  * License along with this library; if not, write to the Free
  21.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  */
  23.  
  24. #include <glib/giochannel.h>
  25. #include <glib/gmain.h>
  26.  
  27. #include <vector>
  28. #include <sigc++/sigc++.h>
  29.  
  30. #include <glibmmconfig.h>
  31. #include <glibmm/refptr.h>
  32. #include <glibmm/timeval.h>
  33.  
  34. GLIBMM_USING_STD(vector)
  35.  
  36.  
  37. namespace Glib
  38. {
  39.  
  40. class Cond;
  41. class Mutex;
  42. class IOChannel;
  43.  
  44.  
  45. /** @defgroup MainLoop The Main Event Loop
  46.  * Manages all available sources of events.
  47.  * @{
  48.  */
  49.  
  50. enum
  51. {
  52.   /*! Use this for high priority event sources.  It is not used within
  53.    * GLib or GTK+.<br><br>
  54.    */
  55.   PRIORITY_HIGH = -100,
  56.  
  57.   /*! Use this for default priority event sources.  In glibmm this
  58.    * priority is used by default when installing timeout handlers with
  59.    * SignalTimeout::connect().  In GDK this priority is used for events
  60.    * from the X server.<br><br>
  61.    */
  62.   PRIORITY_DEFAULT = 0,
  63.  
  64.   /*! Use this for high priority idle functions.  GTK+ uses
  65.    * <tt>PRIORITY_HIGH_IDLE + 10</tt> for resizing operations, and
  66.    * <tt>PRIORITY_HIGH_IDLE + 20</tt> for redrawing operations.
  67.    * (This is done to ensure that any pending resizes are processed before
  68.    * any pending redraws, so that widgets are not redrawn twice unnecessarily.)
  69.    * <br><br>
  70.    */
  71.   PRIORITY_HIGH_IDLE = 100,
  72.  
  73.   /*! Use this for default priority idle functions.  In glibmm this priority is
  74.    * used by default when installing idle handlers with SignalIdle::connect().
  75.    * <br><br>
  76.    */
  77.   PRIORITY_DEFAULT_IDLE = 200,
  78.  
  79.   /*! Use this for very low priority background tasks.  It is not used within
  80.    * GLib or GTK+.
  81.    */
  82.   PRIORITY_LOW = 300
  83. };
  84.  
  85.  
  86. /** A bitwise combination representing an I/O condition to watch for on an
  87.  * event source.
  88.  * The flags correspond to those used by the <tt>%poll()</tt> system call
  89.  * on UNIX (see <tt>man 2 poll</tt>).  To test for individual flags, do
  90.  * something like this:
  91.  * @code
  92.  * if((condition & Glib::IO_OUT) != 0)
  93.  *   do_some_output();
  94.  * @endcode
  95.  * @par Bitwise operators:
  96.  * <tt>IOCondition operator|(IOCondition, IOCondition)</tt><br>
  97.  * <tt>IOCondition operator&(IOCondition, IOCondition)</tt><br>
  98.  * <tt>IOCondition operator^(IOCondition, IOCondition)</tt><br>
  99.  * <tt>IOCondition operator~(IOCondition)</tt><br>
  100.  * <tt>IOCondition& operator|=(IOCondition&, IOCondition)</tt><br>
  101.  * <tt>IOCondition& operator&=(IOCondition&, IOCondition)</tt><br>
  102.  * <tt>IOCondition& operator^=(IOCondition&, IOCondition)</tt><br>
  103.  */
  104. enum IOCondition
  105. {
  106.   IO_IN   = G_IO_IN,  /*!< @hideinitializer There is data to read. */
  107.   IO_OUT  = G_IO_OUT, /*!< @hideinitializer Data can be written (without blocking). */
  108.   IO_PRI  = G_IO_PRI, /*!< @hideinitializer There is urgent data to read. */
  109.   IO_ERR  = G_IO_ERR, /*!< @hideinitializer %Error condition. */
  110.   IO_HUP  = G_IO_HUP, /*!< @hideinitializer Hung up (the connection has been broken,
  111.                                             usually for pipes and sockets). */
  112.   IO_NVAL = G_IO_NVAL /*!< @hideinitializer Invalid request. The file descriptor is not open. */
  113. };
  114.  
  115. inline IOCondition operator|(IOCondition lhs, IOCondition rhs)
  116.   { return static_cast<IOCondition>(static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs)); }
  117.  
  118. inline IOCondition operator&(IOCondition lhs, IOCondition rhs)
  119.   { return static_cast<IOCondition>(static_cast<unsigned>(lhs) & static_cast<unsigned>(rhs)); }
  120.  
  121. inline IOCondition operator^(IOCondition lhs, IOCondition rhs)
  122.   { return static_cast<IOCondition>(static_cast<unsigned>(lhs) ^ static_cast<unsigned>(rhs)); }
  123.  
  124. inline IOCondition operator~(IOCondition flags)
  125.   { return static_cast<IOCondition>(~static_cast<unsigned>(flags)); }
  126.  
  127. inline IOCondition& operator|=(IOCondition& lhs, IOCondition rhs)
  128.   { return (lhs = static_cast<IOCondition>(static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs))); }
  129.  
  130. inline IOCondition& operator&=(IOCondition& lhs, IOCondition rhs)
  131.   { return (lhs = static_cast<IOCondition>(static_cast<unsigned>(lhs) & static_cast<unsigned>(rhs))); }
  132.  
  133. inline IOCondition& operator^=(IOCondition& lhs, IOCondition rhs)
  134.   { return (lhs = static_cast<IOCondition>(static_cast<unsigned>(lhs) ^ static_cast<unsigned>(rhs))); }
  135.  
  136.  
  137. class PollFD
  138. {
  139. public:
  140.   PollFD();
  141.   explicit PollFD(int fd);
  142.   PollFD(int fd, IOCondition events);
  143.  
  144.   void set_fd(int fd) { gobject_.fd = fd;   }
  145.   int  get_fd() const { return gobject_.fd; }
  146.  
  147.   void set_events(IOCondition events)   { gobject_.events = events; }
  148.   IOCondition get_events() const        { return static_cast<IOCondition>(gobject_.events); }
  149.  
  150.   void set_revents(IOCondition revents) { gobject_.revents = revents; }
  151.   IOCondition get_revents() const       { return static_cast<IOCondition>(gobject_.revents); }
  152.  
  153.   GPollFD*       gobj()       { return &gobject_; }
  154.   const GPollFD* gobj() const { return &gobject_; }
  155.  
  156. private:
  157.   GPollFD gobject_;
  158. };
  159.  
  160.  
  161. class SignalTimeout
  162. {
  163. public:
  164. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  165.   explicit inline SignalTimeout(GMainContext* context);
  166. #endif
  167.  
  168.   /** Connects a timeout handler.
  169.    * @code
  170.    * Glib::signal_timeout().connect(sigc::ptr_fun(&timeout_handler), 1000);
  171.    * @endcode
  172.    * is equivalent to:
  173.    * @code
  174.    * const Glib::RefPtr<Glib::TimeoutSource> timeout_source = Glib::TimeoutSource::create(1000);
  175.    * timeout_source->connect(sigc::ptr_fun(&timeout_handler));
  176.    * timeout_source->attach(Glib::MainContext::get_default());
  177.    * @endcode
  178.    * @param slot A slot to call when @a interval elapsed.
  179.    * @param interval The timeout in milliseconds.
  180.    * @param priority The priority of the new event source.
  181.    * @return A connection handle, which can be used to disconnect the handler.
  182.    */
  183.   sigc::connection connect(const sigc::slot<bool>& slot, unsigned int interval,
  184.                            int priority = PRIORITY_DEFAULT);
  185. private:
  186.   GMainContext* context_;
  187.  
  188.   // no copy assignment
  189.   SignalTimeout& operator=(const SignalTimeout&);
  190. };
  191.  
  192.  
  193. class SignalIdle
  194. {
  195. public:
  196. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  197.   explicit inline SignalIdle(GMainContext* context);
  198. #endif
  199.  
  200.   /** Connects an idle handler.
  201.    * @code
  202.    * Glib::signal_idle().connect(sigc::ptr_fun(&idle_handler));
  203.    * @endcode
  204.    * is equivalent to:
  205.    * @code
  206.    * const Glib::RefPtr<Glib::IdleSource> idle_source = Glib::IdleSource::create();
  207.    * idle_source->connect(sigc::ptr_fun(&idle_handler));
  208.    * idle_source->attach(Glib::MainContext::get_default());
  209.    * @endcode
  210.    * @param slot A slot to call when the main loop is idle.
  211.    * @param priority The priority of the new event source.
  212.    * @return A connection handle, which can be used to disconnect the handler.
  213.    */
  214.   sigc::connection connect(const sigc::slot<bool>& slot, int priority = PRIORITY_DEFAULT_IDLE);
  215.  
  216. private:
  217.   GMainContext* context_;
  218.  
  219.   // no copy assignment
  220.   SignalIdle& operator=(const SignalIdle&);
  221. };
  222.  
  223.  
  224. class SignalIO
  225. {
  226. public:
  227. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  228.   explicit inline SignalIO(GMainContext* context);
  229. #endif
  230.  
  231.   /** Connects an I/O handler.
  232.    * @code
  233.    * Glib::signal_io().connect(sigc::ptr_fun(&io_handler), fd, Glib::IO_IN | Glib::IO_HUP);
  234.    * @endcode
  235.    * is equivalent to:
  236.    * @code
  237.    * const Glib::RefPtr<Glib::IOSource> io_source = Glib::IOSource::create(fd, Glib::IO_IN | Glib::IO_HUP);
  238.    * io_source->connect(sigc::ptr_fun(&io_handler));
  239.    * io_source->attach(Glib::MainContext::get_default());
  240.    * @endcode
  241.    * @param slot A slot to call when polling @a fd results in an event that matches @a condition.
  242.    * The event will be passed as a parameter to @a slot.
  243.    * If @a io_handler returns <tt>false</tt> the signal is disconnected.
  244.    * @param fd The file descriptor (or a @c HANDLE on Win32 systems) to watch.
  245.    * @param condition The conditions to watch for.
  246.    * @param priority The priority of the new event source.
  247.    * @return A connection handle, which can be used to disconnect the handler.
  248.    */
  249.   sigc::connection connect(const sigc::slot<bool,IOCondition>& slot, int fd,
  250.                            IOCondition condition, int priority = PRIORITY_DEFAULT);
  251.  
  252.   /** Connects an I/O channel.
  253.    * @code
  254.    * Glib::signal_io().connect(sigc::ptr_fun(&io_handler), channel, Glib::IO_IN | Glib::IO_HUP);
  255.    * @endcode
  256.    * is equivalent to:
  257.    * @code
  258.    * const Glib::RefPtr<Glib::IOSource> io_source = Glib::IOSource::create(channel, Glib::IO_IN | Glib::IO_HUP);
  259.    * io_source->connect(sigc::ptr_fun(&io_handler));
  260.    * io_source->attach(Glib::MainContext::get_default());
  261.    * @endcode
  262.    * @param slot A slot to call when polling @a channel results in an event that matches @a condition.
  263.    * The event will be passed as a parameter to @a slot.
  264.    * If @a io_handler returns <tt>false</tt> the signal is disconnected.
  265.    * @param channel The IOChannel object to watch.
  266.    * @param condition The conditions to watch for.
  267.    * @param priority The priority of the new event source.
  268.    * @return A connection handle, which can be used to disconnect the handler.
  269.    */
  270.   sigc::connection connect(const sigc::slot<bool,IOCondition>& slot, const Glib::RefPtr<IOChannel>& channel,
  271.                            IOCondition condition, int priority = PRIORITY_DEFAULT);
  272.  
  273. private:
  274.   GMainContext* context_;
  275.  
  276.   // no copy assignment
  277.   SignalIO& operator=(const SignalIO&);
  278. };
  279.  
  280. class SignalChildWatch
  281. {
  282. public:
  283. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  284.   explicit inline SignalChildWatch(GMainContext* context);
  285. #endif
  286.   /** Connects a child watch handler.
  287.    * @code
  288.    * Glib::signal_child_watch().connect(sigc::ptr_fun(&child_watch_handler), pid);
  289.    * @endcode
  290.    * @param slot A slot to call when child @a pid exited.
  291.    * @param pid The child to watch for.
  292.    * @param priority The priority of the new event source.
  293.    * @return A connection handle, which can be used to disconnect the handler.
  294.    */
  295.   sigc::connection connect(const sigc::slot<void,GPid, int>& slot, GPid pid, 
  296. int priority = PRIORITY_DEFAULT);
  297. private:
  298.   GMainContext* context_;
  299.  
  300.   // no copy assignment
  301.   SignalChildWatch& operator=(const SignalChildWatch&);
  302. };
  303.  
  304. /** Convenience timeout signal.
  305.  * @return A signal proxy; you want to use SignalTimeout::connect().
  306.  */
  307. SignalTimeout signal_timeout();
  308.  
  309. /** Convenience idle signal.
  310.  * @return A signal proxy; you want to use SignalIdle::connect().
  311.  */
  312. SignalIdle signal_idle();
  313.  
  314. /** Convenience I/O signal.
  315.  * @return A signal proxy; you want to use SignalIO::connect().
  316.  */
  317. SignalIO signal_io();
  318.  
  319. /** Convenience child watch signal.
  320.  * @return A signal proxy; you want to use SignalChildWatch::connect().
  321.  */
  322. SignalChildWatch signal_child_watch();
  323.  
  324.  
  325. /** Main context.
  326.  */
  327. class MainContext
  328. {
  329. public:
  330.   typedef Glib::MainContext  CppObjectType;
  331.   typedef GMainContext       BaseObjectType;
  332.  
  333.   /** Creates a new MainContext.
  334.    * @return The new MainContext.
  335.    */
  336.   static Glib::RefPtr<MainContext> create();
  337.   /** Returns the default main context. 
  338.    * This is the main context used for main loop functions when a main loop is not explicitly specified.
  339.    * @return The new MainContext.
  340.    */
  341.   static Glib::RefPtr<MainContext> get_default();
  342.  
  343.   /** Runs a single iteration for the given main loop. 
  344.    * This involves checking to see if any event sources are ready to be processed, then if no events sources are 
  345.    * ready and may_block is true, waiting for a source to become ready, then dispatching the highest priority events 
  346.    * sources that are ready. Note that even when may_block is true, it is still possible for iteration() to return 
  347.    * false, since the the wait may be interrupted for other reasons than an event source becoming ready.
  348.    * @param may_block Whether the call may block.
  349.    * @return true if events were dispatched.
  350.    */
  351.   bool iteration(bool may_block);
  352.  
  353.   /** Checks if any sources have pending events for the given context.
  354.    * @return true if events are pending.
  355.    */
  356.   bool pending();
  357.  
  358.   /** If context is currently waiting in a poll(), interrupt the poll(), and continue the iteration process.
  359.    */
  360.   void wakeup();
  361.  
  362.   /** Tries to become the owner of the specified context. 
  363.    * If some other context is the owner of the context, returns FALSE immediately. Ownership is properly recursive: 
  364.    * the owner can require ownership again and will release ownership when release() is called as many times as 
  365.    * acquire().
  366.    * You must be the owner of a context before you can call prepare(), query(), check(), dispatch().
  367.    * @return true if the operation succeeded, and this thread is now the owner of context.
  368.    */
  369.   bool acquire();
  370.  
  371.  
  372.   /** Tries to become the owner of the specified context, as with acquire(). But if another thread is the owner, 
  373.    * atomically drop mutex and wait on cond until that owner releases ownership or until cond is signaled, then try 
  374.    * again (once) to become the owner.
  375.    * @param cond A condition variable.
  376.    * @param mutex A mutex, currently held.
  377.    * @return true if the operation succeeded, and this thread is now the owner of context.
  378.    */
  379.   bool wait(Glib::Cond& cond, Glib::Mutex& mutex);
  380.  
  381.   /** Releases ownership of a context previously acquired by this thread with acquire(). If the context was acquired 
  382.    * multiple times, the only release ownership when release() is called as many times as it was acquired.
  383.    */
  384.   void release();
  385.  
  386.  
  387.  
  388.   /** Prepares to poll sources within a main loop. The resulting information for polling is determined by calling query().
  389.    * @param priority Location to store priority of highest priority source already ready.
  390.    * @return true if some source is ready to be dispatched prior to polling.
  391.    */
  392.   bool prepare(int& priority);
  393.   /** Prepares to poll sources within a main loop. The resulting information for polling is determined by calling query().
  394.    * @return true if some source is ready to be dispatched prior to polling.
  395.    */
  396.   bool prepare();
  397.  
  398.   /** Determines information necessary to poll this main loop.
  399.    * @param max_priority Maximum priority source to check.
  400.    * @param timeout Location to store timeout to be used in polling.
  401.    * @param fds Location to store Glib::PollFD records that need to be polled.
  402.    * @return the number of records actually stored in fds, or, if more than n_fds records need to be stored, the number of records that need to be stored.
  403.    */
  404.   void query(int max_priority, int& timeout, std::vector<PollFD>& fds);
  405.  
  406.   /** Passes the results of polling back to the main loop.
  407.    * @param max_priority Maximum numerical priority of sources to check.
  408.    * @param fds Vector of Glib::PollFD's that was passed to the last call to query()
  409.    * @return true if some sources are ready to be dispatched.
  410.    */
  411.   bool check(int max_priority, std::vector<PollFD>& fds);
  412.  
  413.   /** Dispatches all pending sources.
  414.    */
  415.   void dispatch();
  416.  
  417.   //TODO: Use slot instead?
  418.   /** Sets the function to use to handle polling of file descriptors. It will be used instead of the poll() system call (or GLib's replacement function, which is used where poll() isn't available).
  419.    * This function could possibly be used to integrate the GLib event loop with an external event loop.
  420.    * @param poll_func The function to call to poll all file descriptors.
  421.    */
  422.   void set_poll_func(GPollFunc poll_func);
  423.  
  424.   /** Gets the poll function set by g_main_context_set_poll_func().
  425.    * @return The poll function
  426.    */
  427.   GPollFunc get_poll_func();
  428.  
  429.   /** Adds a file descriptor to the set of file descriptors polled for this context. This will very seldomly be used directly. Instead a typical event source will use Glib::Source::add_poll() instead.
  430.    * @param fd A PollFD structure holding information about a file descriptor to watch.
  431.    * @param priority The priority for this file descriptor which should be the same as the priority used for Glib::Source::attach() to ensure that the file descriptor is polled whenever the results may be needed.
  432.    */
  433.   void add_poll(PollFD& fd, int priority);
  434.   
  435.   /** Removes file descriptor from the set of file descriptors to be polled for a particular context.
  436.    * @param fd A PollFD structure holding information about a file descriptor.
  437.    */
  438.   void remove_poll(PollFD& fd);
  439.  
  440.   /** Timeout signal, attached to this MainContext.
  441.    * @return A signal proxy; you want to use SignalTimeout::connect().
  442.    */
  443.   SignalTimeout signal_timeout();
  444.  
  445.   /** Idle signal, attached to this MainContext.
  446.    * @return A signal proxy; you want to use SignalIdle::connect().
  447.    */
  448.   SignalIdle signal_idle();
  449.  
  450.   /** I/O signal, attached to this MainContext.
  451.    * @return A signal proxy; you want to use SignalIO::connect().
  452.    */
  453.   SignalIO signal_io();
  454.  
  455.   /** child watch signal, attached to this MainContext.
  456.    * @return A signal proxy; you want to use SignalChildWatch::connect().
  457.    */
  458.   SignalChildWatch signal_child_watch();
  459.  
  460.   void reference()   const;
  461.   void unreference() const;
  462.  
  463.   GMainContext*       gobj();
  464.   const GMainContext* gobj() const;
  465.   GMainContext*       gobj_copy() const;
  466.  
  467. private:
  468.   // Glib::MainContext can neither be constructed nor deleted.
  469.   MainContext();
  470.   void operator delete(void*, size_t);
  471.  
  472.   // noncopyable
  473.   MainContext(const MainContext& other);
  474.   MainContext& operator=(const MainContext& other);
  475.  
  476. };
  477.  
  478. /** @relates Glib::MainContext */
  479. Glib::RefPtr<MainContext> wrap(GMainContext* gobject, bool take_copy = false);
  480.  
  481.  
  482. class MainLoop
  483. {
  484. public:
  485.   typedef Glib::MainLoop  CppObjectType;
  486.   typedef GMainLoop       BaseObjectType;
  487.  
  488.   static Glib::RefPtr<MainLoop> create(bool is_running = false);
  489.   static Glib::RefPtr<MainLoop> create(const Glib::RefPtr<MainContext>& context,
  490.                                        bool is_running = false);
  491.  
  492.   /** Runs a main loop until quit() is called on the loop. 
  493.    * If this is called for the thread of the loop's MainContext, it will process events from the loop, otherwise it will simply wait.
  494.    */
  495.   void run();
  496.  
  497.   /** Stops a MainLoop from running. Any calls to run() for the loop will return.
  498.    */
  499.   void quit();
  500.  
  501.   /** Checks to see if the main loop is currently being run via run().
  502.    * @return true if the mainloop is currently being run.
  503.    */
  504.   bool is_running();
  505.  
  506.   /** Returns the MainContext of loop.
  507.    * @return The MainContext of loop.
  508.    */
  509.   Glib::RefPtr<MainContext> get_context();
  510.  
  511.   //TODO: C++ize the (big) g_main_depth docs here.
  512.   static int depth();
  513.  
  514.   /** Increases the reference count on a MainLoop object by one.
  515.    */
  516.   void reference()   const;
  517.  
  518.   /** Decreases the reference count on a MainLoop object by one. 
  519.    * If the result is zero, free the loop and free all associated memory.
  520.    */
  521.   void unreference() const;
  522.  
  523.   GMainLoop*       gobj();
  524.   const GMainLoop* gobj() const;
  525.   GMainLoop*       gobj_copy() const;
  526.  
  527. private:
  528.   // Glib::MainLoop can neither be constructed nor deleted.
  529.   MainLoop();
  530.   void operator delete(void*, size_t);
  531.  
  532.   MainLoop(const MainLoop&);
  533.   MainLoop& operator=(const MainLoop&);
  534. };
  535.  
  536. /** @relates Glib::MainLoop */
  537. Glib::RefPtr<MainLoop> wrap(GMainLoop* gobject, bool take_copy = false);
  538.  
  539.  
  540. class Source
  541. {
  542. public:
  543.   typedef Glib::Source  CppObjectType;
  544.   typedef GSource       BaseObjectType;
  545.  
  546.   static Glib::RefPtr<Source> create() /* = 0 */;
  547.  
  548.   /** Adds a Source to a context so that it will be executed within that context.
  549.    * @param context A MainContext.
  550.    * @return The ID for the source within the MainContext.
  551.    */
  552.   unsigned int attach(const Glib::RefPtr<MainContext>& context);
  553.  
  554.   /** Adds a Source to a context so that it will be executed within that context.
  555.    * The default context will be used.
  556.    * @return The ID for the source within the MainContext.
  557.    */
  558.   unsigned int attach();
  559.  
  560.   //TODO: Does this destroy step make sense in C++? Should it just be something that happens in a destructor?
  561.   
  562.   /** Removes a source from its MainContext, if any, and marks it as destroyed. 
  563.    * The source cannot be subsequently added to another context.
  564.    */
  565.   void destroy();
  566.  
  567.   /** Sets the priority of a source. While the main loop is being run, a source will be dispatched if it is ready to be dispatched and no sources at a higher (numerically smaller) priority are ready to be dispatched.
  568.    * @param priority The new priority.
  569.    */
  570.   void set_priority(int priority);
  571.   
  572.   /** Gets the priority of a source.
  573.    * @return The priority of the source.
  574.    */
  575.   int  get_priority() const;
  576.  
  577.   /** Sets whether a source can be called recursively. 
  578.    * If @a can_recurse is true, then while the source is being dispatched then this source will be processed normally. Otherwise, all processing of this source is blocked until the dispatch function returns.
  579.    * @param can_recurse Whether recursion is allowed for this source.
  580.    */
  581.   void set_can_recurse(bool can_recurse);
  582.   
  583.   /** Checks whether a source is allowed to be called recursively. see set_can_recurse().
  584.    * @return Whether recursion is allowed.
  585.    */
  586.   bool get_can_recurse() const;
  587.  
  588.   /** Returns the numeric ID for a particular source. 
  589.    * The ID of a source is unique within a particular main loop context. The reverse mapping from ID to source is done by MainContext::find_source_by_id().
  590.    * @return The ID for the source.
  591.    */
  592.   unsigned int get_id() const;
  593.  
  594.   //TODO: Add a const version of this method?
  595.   /** Gets the MainContext with which the source is associated. 
  596.    * Calling this function on a destroyed source is an error.
  597.    * @return The MainContext with which the source is associated, or a null RefPtr if the context has not yet been added to a source. 
  598.    */
  599.   Glib::RefPtr<MainContext> get_context();
  600.  
  601.   GSource*       gobj()       { return gobject_; }
  602.   const GSource* gobj() const { return gobject_; }
  603.   GSource*       gobj_copy() const;
  604.  
  605.   void reference()   const;
  606.   void unreference() const;
  607.  
  608. protected:
  609.   /** Construct an object that uses the virtual functions prepare(), check() and dispatch().
  610.    */
  611.   Source();
  612.  
  613.   /** Wrap an existing GSource object and install the given callback function.
  614.    * The constructed object doesn't use the virtual functions prepare(), check() and dispatch().
  615.    * This constructor is for use by derived types that need to wrap a GSource object.
  616.    * The callback function can be a static member function. But beware -
  617.    * depending on the actual implementation of the GSource's virtual functions
  618.    * the expected type of the callback function can differ from GSourceFunc.
  619.    */
  620.   Source(GSource* cast_item, GSourceFunc callback_func);
  621.  
  622.   virtual ~Source();
  623.  
  624.   sigc::connection connect_generic(const sigc::slot_base& slot);
  625.  
  626.   /** Adds a file descriptor to the set of file descriptors polled for this source. 
  627.    * The event source's check function will typically test the revents field in the PollFD  and return true if events need to be processed.
  628.    * @param poll_fd A PollFD object holding information about a file descriptor to watch.
  629.    */
  630.   void add_poll(PollFD& poll_fd);
  631.   
  632.   /** Removes a file descriptor from the set of file descriptors polled for this source.
  633.    * @param poll_fd A PollFD object previously passed to add_poll(). 
  634.    */
  635.   void remove_poll(PollFD& poll_fd);
  636.  
  637.   /** Gets the "current time" to be used when checking this source. The advantage of calling this function over calling get_current_time() directly is that when checking multiple sources, GLib can cache a single value instead of having to repeatedly get the system time.
  638.    * @param current_time Glib::TimeVal in which to store current time
  639.    */
  640.   void get_current_time(Glib::TimeVal& current_time);
  641.  
  642.   virtual bool prepare(int& timeout) = 0;
  643.   virtual bool check() = 0;
  644.   virtual bool dispatch(sigc::slot_base* slot) = 0;
  645.  
  646. private:
  647.   GSource* gobject_;
  648.  
  649. #ifndef DOXGEN_SHOULD_SKIP_THIS
  650.  
  651.   static inline Source* get_wrapper(GSource* source);
  652.  
  653.   static const GSourceFuncs vfunc_table_;
  654.  
  655.   static gboolean prepare_vfunc(GSource* source, int* timeout);
  656.   static gboolean check_vfunc(GSource* source);
  657.   static gboolean dispatch_vfunc(GSource* source, GSourceFunc callback, void* user_data);
  658. public:
  659.   static void destroy_notify_callback(void* data);
  660. private:
  661.  
  662. #endif /* DOXGEN_SHOULD_SKIP_THIS */
  663.  
  664.   // noncopyable
  665.   Source(const Source&);
  666.   Source& operator=(const Source&);
  667. };
  668.  
  669.  
  670. class TimeoutSource : public Glib::Source
  671. {
  672. public:
  673.   typedef Glib::TimeoutSource CppObjectType;
  674.  
  675.   static Glib::RefPtr<TimeoutSource> create(unsigned int interval);
  676.   sigc::connection connect(const sigc::slot<bool>& slot);
  677.  
  678. protected:
  679.   explicit TimeoutSource(unsigned int interval);
  680.   virtual ~TimeoutSource();
  681.  
  682.   virtual bool prepare(int& timeout);
  683.   virtual bool check();
  684.   virtual bool dispatch(sigc::slot_base* slot);
  685.  
  686. private:
  687.   Glib::TimeVal expiration_;
  688.   unsigned int  interval_;
  689. };
  690.  
  691.  
  692. class IdleSource : public Glib::Source
  693. {
  694. public:
  695.   typedef Glib::IdleSource CppObjectType;
  696.  
  697.   static Glib::RefPtr<IdleSource> create();
  698.   sigc::connection connect(const sigc::slot<bool>& slot);
  699.  
  700. protected:
  701.   IdleSource();
  702.   virtual ~IdleSource();
  703.  
  704.   virtual bool prepare(int& timeout);
  705.   virtual bool check();
  706.   virtual bool dispatch(sigc::slot_base* slot_data);
  707. };
  708.  
  709.  
  710. class IOSource : public Glib::Source
  711. {
  712. public:
  713.   typedef Glib::IOSource CppObjectType;
  714.  
  715.   static Glib::RefPtr<IOSource> create(int fd, IOCondition condition);
  716.   static Glib::RefPtr<IOSource> create(const Glib::RefPtr<IOChannel>& channel, IOCondition condition);
  717.   sigc::connection connect(const sigc::slot<bool,IOCondition>& slot);
  718.  
  719. protected:
  720.   IOSource(int fd, IOCondition condition);
  721.   IOSource(const Glib::RefPtr<IOChannel>& channel, IOCondition condition);
  722.   virtual ~IOSource();
  723.  
  724.   virtual bool prepare(int& timeout);
  725.   virtual bool check();
  726.   virtual bool dispatch(sigc::slot_base* slot);
  727.  
  728. private:
  729.   PollFD poll_fd_;
  730. };
  731.  
  732. /** @} group MainLoop */
  733.  
  734. } // namespace Glib
  735.  
  736.  
  737. #endif /* _GLIBMM_MAIN_H */
  738.  
  739.